﻿using SHZY.Common.Core.Manager;
using SHZY.Common.Core.Security;
using SHZY.Common.Enum;
using SHZY.Common.Extension;
using SHZY.Common.Filter;
using SHZY.Common.Models;
using SHZY.Common.Security;
using SHZY.DependencyInjection;
using SHZY.DynamicApiController;
using SHZY.FriendlyException;
using SHZY.Systems.Entitys.Permission;
using SHZY.Systems.Entitys.System;
using SHZY.Systems.Interfaces.System;
using SHZY.system.Entitys.Dto.TestProjects;
using SHZY.system.Entitys;
using SHZY.system.Interfaces;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace SHZY.system;

/// <summary>
/// 业务实现：项目立项申请.
/// </summary>
[ApiDescriptionSettings(Tag = "system", Name = "TestProjects", Order = 200)]
[Route("api/system/[controller]")]
public class TestProjectsService : ITestProjectsService, IDynamicApiController, ITransient
{
    /// <summary>
    /// 服务基础仓储.
    /// </summary>
    private readonly ISqlSugarRepository<TestProjectsEntity> _repository;

    /// <summary>
    /// 单据规则服务.
    /// </summary>
    private readonly IBillRullService _billRullService;

    /// <summary>
    /// 数据库管理.
    /// </summary>
    private readonly IDataBaseManager _dataBaseManager;

    /// <summary>
    /// 数据接口服务.
    /// </summary>
    private readonly IDataInterfaceService _dataInterfaceService;

    /// <summary>
    /// 用户管理.
    /// </summary>
    private readonly IUserManager _userManager;

    /// <summary>
    /// 客户端.
    /// </summary>
    private SqlSugarClient _sqlSugarClient;

    /// <summary>
    /// 初始化一个<see cref="TestProjectsService"/>类型的新实例.
    /// </summary>
    public TestProjectsService(
        ISqlSugarRepository<TestProjectsEntity> testProjectsRepository,
        IBillRullService billRullService,
        IDataInterfaceService dataInterfaceService,
        IDataBaseManager dataBaseManager,
        ISqlSugarClient context,
        IUserManager userManager)
    {
        _repository = testProjectsRepository;
        _billRullService = billRullService;
        _dataBaseManager = dataBaseManager;
        _sqlSugarClient = (SqlSugarClient)context;
        _dataInterfaceService = dataInterfaceService;
        _userManager = userManager;
    }

    /// <summary>
    /// 获取项目立项申请.
    /// </summary>
    /// <param name="id">主键值.</param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<dynamic> GetInfo(string id)
    {
        var dbLink = await _repository.Context.Queryable<DbLinkEntity>().FirstAsync(it => it.Id.Equals(""));
        _sqlSugarClient = _dataBaseManager.ChangeDataBase(dbLink);
            return (await _sqlSugarClient.Queryable<TestProjectsEntity>().FirstAsync(it => it.Id == id)).Adapt<TestProjectsInfoOutput>();
    }

    /// <summary>
    /// 新建项目立项申请.
    /// </summary>
    /// <param name="input">参数.</param>
    /// <returns></returns>
    [HttpPost("")]
    public async Task Create([FromBody] TestProjectsCrInput input)
    {
        var entity = input.Adapt<TestProjectsEntity>();
        entity.Id = SnowflakeIdHelper.NextId();
        entity.Billno = await _billRullService.GetBillNumber("dingdanbianhao1");
        var dbLink = await _repository.Context.Queryable<DbLinkEntity>().FirstAsync(it => it.Id.Equals(""));
        _sqlSugarClient = _dataBaseManager.ChangeDataBase(dbLink);
        var isOk = await _sqlSugarClient.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
        if (!(isOk > 0)) throw Oops.Oh(ErrorCode.COM1000);
    }

    /// <summary>
    /// 更新项目立项申请.
    /// </summary>
    /// <param name="id">主键值.</param>
    /// <param name="input">参数.</param>
    /// <returns></returns>
    [HttpPut("{id}")]
    public async Task Update(string id, [FromBody] TestProjectsUpInput input)
    {
        var entity = input.Adapt<TestProjectsEntity>();
        var dbLink = await _repository.Context.Queryable<DbLinkEntity>().FirstAsync(it => it.Id.Equals(""));
        _sqlSugarClient = _dataBaseManager.ChangeDataBase(dbLink);
        var isOk = await _sqlSugarClient.Updateable(entity).UpdateColumns(it => new
        {
            it.Projectname,
            it.Projecttype,
            it.Projectcode,
            it.Projectfunding,
            it.Depname,
            it.Person,
            it.Degree,
            it.Billno,
        }).ExecuteCommandAsync();
        if (!(isOk > 0)) throw Oops.Oh(ErrorCode.COM1001);
    }

    /// <summary>
    /// 删除项目立项申请.
    /// </summary>
    /// <returns></returns>
    [HttpDelete("{id}")]
    public async Task Delete(string id)
    {
        var dbLink = await _repository.Context.Queryable<DbLinkEntity>().FirstAsync(it => it.Id.Equals(""));
        _sqlSugarClient = _dataBaseManager.ChangeDataBase(dbLink);
        var isOk = await _sqlSugarClient.Deleteable<TestProjectsEntity>().Where(it => it.Id.Equals(id)).ExecuteCommandAsync();
        if (!(isOk > 0)) throw Oops.Oh(ErrorCode.COM1002);
    }
}